package com.xysd.express.activity.logistics;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.xysd.express.R;
import com.xysd.express.adapter.map.searchAdapter;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.map.SearchModel;
import com.xysd.express.eventbus.LocationEvent;
import com.xysd.express.rx.RxBus;
import com.xysd.express.util.ActivityTaskManeger;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_pio)
public class PioActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.PioRecycler)
    private RecyclerView PioRecycler;

    @ViewInject(R.id.piosearch_et)
    private EditText piosearch_et;
    private searchAdapter adapter;


    private String type;

    @Override
    protected void init() {
        setTitleBar("选择地址");
        type=getIntent().getStringExtra("type");
        piosearch_et.addTextChangedListener(this);
        adapter = new searchAdapter(R.layout.map_item_layout, this, 1);
        PioRecycler.setLayoutManager(new LinearLayoutManager(this));
        PioRecycler.setAdapter(adapter);
        adapter.setCall(new searchAdapter.itemCall() {
            @Override
            public void itemClick(SearchModel item) {
                RxBus.getInstance().post(new LocationEvent(item,type));
                ActivityTaskManeger.getInstance().closeActivity(MapActivity.class);
                ActivityTaskManeger.getInstance().closeActivity();
            }
        });
    }

    private List<SearchModel> models = new ArrayList<>();

    protected void doSearchQuery(final String address) {
        PoiSearch.Query query = new PoiSearch.Query(address, "", "");
        final PoiSearch search = new PoiSearch(this, query);
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);
        search.setOnPoiSearchListener(
                new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int errcode) {
                        if (errcode == 1000) {
                            if (null != poiResult && poiResult.getPois().size() > 0) {
                                models.clear();
                                for (int i = 0; i < poiResult.getPois().size(); i++) {
                                    models.add(new SearchModel(address, poiResult.getPois().get(i).getAdName()
                                            + poiResult.getPois().get(i).getSnippet()
                                            + poiResult.getPois().get(i).getTitle(),
                                            poiResult.getPois().get(i).getLatLonPoint().getLongitude() + "",
                                            poiResult.getPois().get(i).getLatLonPoint().getLatitude() + ""));
                                }
                                adapter.setNewData(models);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {
                    }
                }
        );
        search.searchPOIAsyn();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        doSearchQuery(editable.toString());
    }
}
